package com.qf.business.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.business.system.mapper.RoleMapper;
import com.qf.business.system.service.RolePowerTableService;
import com.qf.data.system.dto.RoleDto;
import com.qf.data.system.entity.Role;
import com.qf.business.system.service.RoleService;
import com.qf.data.system.entity.RolePowerTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePowerTableService rolePowerTableService;

    @Override
    public List<RoleDto> queryList() {
        return roleMapper.queryList();
    }

    @Override
    public int updatePowerByRid(Long rid, List<Long> pids) {
        rolePowerTableService.removeById(rid);
        //先删除，再插入--批量插入
        List<RolePowerTable> rolePowerTables = new ArrayList<>();
        for (Long pid : pids) {
            RolePowerTable rolePowerTable = new RolePowerTable()
                    .setRid(rid)
                    .setPid(pid);
            rolePowerTables.add(rolePowerTable);
        }

        rolePowerTableService.saveBatch(rolePowerTables);
        return 1;
    }
}

